package firstline.example.andy.notebook;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by Andy on 2015/7/14.
 */
public class Edit extends Activity implements View.OnClickListener {

    private Button button;
    private EditText title,content;
    private DBhelper dBhelper;
    private SQLiteDatabase db;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.edit);
        initView();
    }

    private void initView() {
        button= (Button) findViewById(R.id.button);
        title= (EditText) findViewById(R.id.title);
        content= (EditText) findViewById(R.id.content);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        dBhelper=new DBhelper(this,"liuyanban.db");
        //创建数据库成功
        db=dBhelper.getReadableDatabase();
        String ti=title.getText().toString();
        String co=content.getText().toString();
        long date=System.currentTimeMillis();
        ContentValues cv=new ContentValues();
        cv.put("title", ti);
        cv.put("content", co);
        cv.put("time", date);
        Log.i("date>>>>>", "标题:" + ti + "内容:" + co + "时间:" + date);
        db.insert("liuyan", null, cv);
        Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
        Intent intent=new Intent();
        intent.putExtra("title",ti) ;
        intent.putExtra("content",co);
        intent.putExtra("time",date);
        setResult(1,intent);
        finish();
    }
}
